package com.learn.springcloud.eurekahaconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * @Time : 2018/7/23 20:02
 * @Version : 1.0
 * @Author : hongyan.li@hand-china.com
 * Copyright @ 2000 Shanghai Hand Co. Ltd.
 * All right reserved.
 */
@RestController
public class DeleteConsumer {


    @Autowired
    private RestTemplate restTemplate;


    @DeleteMapping(value = "/consumer/delete/example1/{name}")
    public String deleteExample1(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/delete/service/{name}";
        restTemplate.delete(url,name);
        return "success";
    }


    @DeleteMapping(value = "/consumer/delete/example2/{name}")
    public String deleteExample2(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/delete/service/{name}";
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        restTemplate.delete(url,map);
        return "success";
    }


    @DeleteMapping(value = "/consumer/delete/example3/{name}")
    public String deleteExample3(@PathVariable String name){
        String url = "http://ribbon-resttemplate-example-provider/provider/delete/service/{name}";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand(name).encode();
        restTemplate.delete(uriComponents.toUri());
        return "success";
    }


}
